package com.wdt.data.controller;


import com.wdt.data.model.BasHeatsource;
import com.wdt.data.util.R;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/upload")
public class UploadFileToDBCrontroller {
    private List<BasHeatsource> list = null;

    @RequestMapping("/to_upload")
    public R uploadFile(MultipartFile file, HttpServletRequest request) {
        Map result = null;
        try {
            String path = "D:/file/excel";

            //创建文件
            File dir = new File(path);
            if (!dir.exists()) {
                dir.mkdirs();
            }

            String fileName = file.getOriginalFilename();


            fileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));//zhao.jpg
            FileOutputStream imgOut = new FileOutputStream(new File(dir, fileName));//根据 dir 抽象路径名和 img 路径名字符串创建一个新 File 实例。
            /* System.out.println(file.getBytes());*/
            imgOut.write(file.getBytes());//返回一个字节数组文件的内容
            imgOut.close();


            InputStream is = file.getInputStream();
//            result = new PoiUtil().readExcelFileForHeatSource(is, path + File.separator + fileName);
            list = (ArrayList<BasHeatsource>) result.get("list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok(result);
    }

    @RequestMapping("/test_upload")
    public void test() {
        System.out.println(list.toString());
    }
}
